{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.2"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 类型转换"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "- 将一个类型的对象转换另一个类型的对象\n",
    "- 类型转换不是改变对象本身的类型，而是根据对象的值创建新的对象\n",
    "- 类型转换 4 个函数\n",
    "    - `int()`\n",
    "    - `float()`\n",
    "    - `str()`\n",
    "    - `bool()`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "## `int()`\n",
    "\n",
    "`int()` 不会对原来的变量产生影响，它会将对象转换为指定的类型并将其作为返回值返回\n",
    "\n",
    "### 规则\n",
    "\n",
    "- 布尔值 - `True` -> `0`, `Flase` -> `0`\n",
    "- 浮点数 - 取整\n",
    "- 字符串\n",
    "    - 合法的整数字符串直接转化为对应的数字\n",
    "    - 如果不是则报错 `ValueError: invalid literal for int() with base 10`\n",
    "- 对于其它不可转换为整型的对象，直接抛出异常 `TypeError: int() argument must be a string, a bytes-like object or a number`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True\n",
    "b = False\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = int(a)\n",
    "b = int(b)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1.99\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "a = int(a)\n",
    "print(f'a = {a}, type = {type(a)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = '123'\n",
    "b = '123.33'\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = int(a)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "b = int(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = None\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "a = int(a)"
   ]
  },
  {
   "source": [
    "## `float()`\n",
    "\n",
    "`float()` 和 `int()` 基本一致，不同的是它会将对象转化为浮点数"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True\n",
    "b = False\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = float(a)\n",
    "b = float(b)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "a = float(a)\n",
    "print(f'a = {a}, type = {type(a)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = '123'\n",
    "b = '123.33'\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = float(a)\n",
    "b = float(b)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = None\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "a = float(a)"
   ]
  },
  {
   "source": [
    "## `str()`\n",
    "\n",
    "`str()` 可以将对象转换成字符串"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = True\n",
    "b = False\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = str(a)\n",
    "b = str(b)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = 1.23\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = str(a)\n",
    "b = str(b)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = None\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "a = str(a)\n",
    "print(f'a = {a}, type = {type(a)}')"
   ]
  },
  {
   "source": [
    "## `bool()`\n",
    "\n",
    "对于所有表示空性的对象都会转化为 `False`, 其余的转化为 `True`\n",
    "\n",
    "表示空性的对象：\n",
    "\n",
    "- `0`\n",
    "- `''`\n",
    "- `None`"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 1\n",
    "b = -1\n",
    "c = 0\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "print(f'b = {c}, type = {type(c)}')\n",
    "a = bool(a)\n",
    "b = bool(b)\n",
    "c = bool(c)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "print(f'b = {c}, type = {type(c)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'Hello'\n",
    "b = ''\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')\n",
    "a = bool(a)\n",
    "b = bool(b)\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "print(f'b = {b}, type = {type(b)}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = None\n",
    "print(f'a = {a}, type = {type(a)}')\n",
    "a = bool(a)\n",
    "print(f'a = {a}, type = {type(a)}')"
   ]
  }
 ]
}